From 845d86eae334ff392371e1cb556be8a5b39fe4c0 Mon Sep 17 00:00:00 2001 From: Michael Uno Date: Sat, 30 Sep 2023 14:04:06 +0900 Subject: [PATCH] Feature: Add the ability to make the Add to Cart button a mere external link UI is not implemented yet and with this change it forces the plugin to use this feature at the moment. Resolves #5 --- auto-amazon-links-woocommerce-products.php | 1 + includes/c.cart-to-link/Loader.php | 28 ++++ .../c.cart-to-link/events/CustomCartLinks.php | 127 ++++++++++++++++++ includes/class-map.php | 2 + 4 files changed, 158 insertions(+) create mode 100644 includes/c.cart-to-link/Loader.php create mode 100644 includes/c.cart-to-link/events/CustomCartLinks.php diff --git a/auto-amazon-links-woocommerce-products.php b/auto-amazon-links-woocommerce-products.php index 86ae5f9..1ea237f 100644 --- a/auto-amazon-links-woocommerce-products.php +++ b/auto-amazon-links-woocommerce-products.php @@ -33,6 +33,7 @@ class App { public $aMembers = [ __NAMESPACE__ . '\\Converter\\Loader' => null, __NAMESPACE__ . '\\Buttons\\Loader' => null, + __NAMESPACE__ . '\\CartToLink\\Loader' => null, __NAMESPACE__ . '\\Events\\Filters\\ProductThumbnails' => null, __NAMESPACE__ . '\\Events\\Filters\\PriceHTML' => null, __NAMESPACE__ . '\\Events\\Redirects\\Checkout' => null, diff --git a/includes/c.cart-to-link/Loader.php b/includes/c.cart-to-link/Loader.php new file mode 100644 index 0000000..b87e5f8 --- /dev/null +++ b/includes/c.cart-to-link/Loader.php @@ -0,0 +1,28 @@ + null, + ]; + +} \ No newline at end of file diff --git a/includes/c.cart-to-link/events/CustomCartLinks.php b/includes/c.cart-to-link/events/CustomCartLinks.php new file mode 100644 index 0000000..5d96a3c --- /dev/null +++ b/includes/c.cart-to-link/events/CustomCartLinks.php @@ -0,0 +1,127 @@ +` tag. + */ + public function replyToEditAddToCartButtonLinkInLoops( $sLink, $oProduct, $aArguments ) { + return "" + . $this->___getCartButtonLabel() + . ""; + } + + /** + * @since 1.1.0 + * @return string + */ + public function replyToEditAddToCartTextSingle( $sButtonText, $oProduct ) { + return $this->___getCartButtonLabel(); + } + + /** + * @since 1.1.0 + * @return string + */ + public function replyToEditAddToCartTextInLoops( $sButtonText, $oProduct ) { + return $this->___getCartButtonLabel(); + } + + /** + * @since 1.1.0 + * @return void + */ + public function replyToAddBeforeCartForm() { + echo ""; // end hidden + + if ( empty( $GLOBALS[ 'product' ] ) ) { + return; + } + + // Add the "Add to Cart" link + echo "
" + . "get_title() ) . "'>" + . $this->___getCartButtonLabel() + . "" + . "
"; + } + + /** + * Modifies the Add to Cart URL + * + * The Store Front WooCommerce default theme adds a sticky form at the top and this filter is needed for it. + * + * @param string $sPermalink + * @param \WC_Product $oProduct + * @return string + * @sinec 1.1.0 + */ + public function replyToEditAddToCartURL( $sPermalink, $oProduct ) { + return $this->___getCartHref( $oProduct ); + } + + /** + * @since 1.1.0 + * @return string + */ + private function ___getCartButtonLabel() { + return __( 'Buy Now', 'amazon-auto-links' ); + } + + /** + * @since 1.1.0 + * @param \WC_Product $oProduct + * @return string + */ + private function ___getCartHref( $oProduct ) { + $_oUtil = new \AmazonAutoLinks_PluginUtility(); + $_sSKU = $oProduct->get_sku(); // ASIN|locale|currency|language + $_aItem = explode( '|', $_sSKU ); + $_sASIN = $_oUtil->getElement( $_aItem, 0 ); + $_sLocale = $_oUtil->getElement( $_aItem, 1 ); + $_oOption = \AmazonAutoLinks_Option::getInstance(); + $_oLocale = new \AmazonAutoLinks_Locale( $_sLocale ); + return $_oLocale->getMarketPlaceURL( 'dp/' . $_sASIN . '/?tag=' . $_oOption->getAssociateID( $_sLocale ) ); + } + +} \ No newline at end of file diff --git a/includes/class-map.php b/includes/class-map.php index 07ea15e..430517f 100644 --- a/includes/class-map.php +++ b/includes/class-map.php @@ -4,6 +4,8 @@ "AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/Loader.php", "AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Events\\DynamicProductCreation" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/events/DynamicProductCreation.php", "AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Events\\ItemFormatTags" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/events/ItemFormatTags.php", + "AutoAmazonLinks\\WooCommerceProducts\\CartToLink\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.cart-to-link/Loader.php", + "AutoAmazonLinks\\WooCommerceProducts\\CartToLink\\Events\\CustomCartLinks" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.cart-to-link/events/CustomCartLinks.php", "AutoAmazonLinks\\WooCommerceProducts\\Converter\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/Loader.php", "AutoAmazonLinks\\WooCommerceProducts\\Converter\\Admin\\FormFields\\Main" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/admin/form-fields/Main.php", "AutoAmazonLinks\\WooCommerceProducts\\Converter\\Admin\\PostMetaBoxes\\Main" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/admin/post-meta-boxes/Main.php",